/**
 * Bob 语言代码映射
 * 参考 https://bobtranslate.com/plugin/addition/language.html
 */
var items = [
    ['auto','auto'],
    ['zh-Hans','zh-Hans'],
    ['zh-Hant','zh-Hant'],
    ['yue','yue'],
    ['wyw','wyw'],
    ['pysx','pysx'],
    ['en','en'],
    ['ja','ja'],
    ['ko','ko'],
    ['fr','fr'],
    ['de','de'],
    ['es','es'],
    ['it','it'],
    ['ru','ru'],
    ['pt','pt'],
    ['nl','nl'],
    ['pl','pl'],
    ['ar','ar'],
    ['ab','ab'],
    ['ace','ace'],
    ['acr','acr'],
    ['acu','acu'],
    ['adh','adh'],
    ['af','af'],
    ['agr','agr'],
    ['ak','ak'],
    ['ake','ake'],
    ['am','am'],
    ['amk','amk'],
    ['amu','amu'],
    ['an','an'],
    ['any','any'],
    ['as','as'],
    ['ast','ast'],
    ['ata','ata'],
    ['ay','ay'],
    ['az','az'],
    ['azb','azb'],
    ['ba','ba'],
    ['bal','bal'],
    ['bam','bam'],
    ['bba','bba'],
    ['bch','bch'],
    ['bdh','bdh'],
    ['bdu','bdu'],
    ['be','be'],
    ['bem','bem'],
    ['ber','ber'],
    ['bfa','bfa'],
    ['bg','bg'],
    ['bho','bho'],
    ['bi','bi'],
    ['bm','bm'],
    ['bn','bn'],
    ['bno','bno'],
    ['bnp','bnp'],
    ['bo','bo'],
    ['bqj','bqj'],
    ['bqp','bqp'],
    ['br','br'],
    ['bs','bs'],
    ['bsn','bsn'],
    ['bus','bus'],
    ['byn','byn'],
    ['byr','byr'],
    ['ca','ca'],
    ['cak','cak'],
    ['cas','cas'],
    ['cbk','cbk'],
    ['cbl','cbl'],
    ['ccp','ccp'],
    ['cdf','cdf'],
    ['ceb','ceb'],
    ['cfm','cfm'],
    ['ch','ch'],
    ['che','che'],
    ['chm','chm'],
    ['chq','chq'],
    ['chr','chr'],
    ['cjp','cjp'],
    ['ckb','ckb'],
    ['cki','cki'],
    ['cnh','cnh'],
    ['cni','cni'],
    ['cnr','cnr'],
    ['co','co'],
    ['cop','cop'],
    ['cpb','cpb'],
    ['crh','crh'],
    ['crs','crs'],
    ['cs','cs'],
    ['csb','csb'],
    ['ctd','ctd'],
    ['cv','cv'],
    ['cy','cy'],
    ['czt','czt'],
    ['da','da'],
    ['dik','dik'],
    ['dje','dje'],
    ['djk','djk'],
    ['dop','dop'],
    ['dtp','dtp'],
    ['duo','duo'],
    ['dv','dv'],
    ['dyu','dyu'],
    ['dz','dz'],
    ['ee','ee'],
    ['el','el'],
    ['enx','enx'],
    ['eo','eo'],
    ['et','et'],
    ['eu','eu'],
    ['fa','fa'],
    ['fi','fi'],
    ['fil','fil'],
    ['fj','fj'],
    ['fo','fo'],
    ['fur','fur'],
    ['fuv','fuv'],
    ['fvr','fvr'],
    ['fy','fy'],
    ['ga','ga'],
    ['gbi','gbi'],
    ['gbo','gbo'],
    ['gd','gd'],
    ['gil','gil'],
    ['gl','gl'],
    ['gn','gn'],
    ['gnw','gnw'],
    ['gof','gof'],
    ['gos','gos'],
    ['gu','gu'],
    ['gub','gub'],
    ['gur','gur'],
    ['gv','gv'],
    ['ha','ha'],
    ['haw','haw'],
    ['hbs','hbs'],
    ['he','he'],
    ['hi','hi'],
    ['hil','hil'],
    ['hlb','hlb'],
    ['hmn','hmn'],
    ['hmo','hmo'],
    ['hr','hr'],
    ['hsb','hsb'],
    ['ht','ht'],
    ['hu','hu'],
    ['hui','hui'],
    ['hup','hup'],
    ['huv','huv'],
    ['hwc','hwc'],
    ['hy','hy'],
    ['ia','ia'],
    ['iba','iba'],
    ['id','id'],
    ['ie','ie'],
    ['ifa','ifa'],
    ['ifb','ifb'],
    ['ify','ify'],
    ['ig','ig'],
    ['ikk','ikk'],
    ['ilo','ilo'],
    ['inh','inh'],
    ['io','io'],
    ['iou','iou'],
    ['is','is'],
    ['iu','iu'],
    ['izz','izz'],
    ['jac','jac'],
    ['jae','jae'],
    ['jbo','jbo'],
    ['jiv','jiv'],
    ['jmc','jmc'],
    ['jv','jv'],
    ['jw','jw'],
    ['jy','jy'],
    ['ka','ka'],
    ['kab','kab'],
    ['kac','kac'],
    ['kbh','kbh'],
    ['kbo','kbo'],
    ['kdx','kdx'],
    ['kek','kek'],
    ['keo','keo'],
    ['kg','kg'],
    ['kha','kha'],
    ['ki','ki'],
    ['kj','kj'],
    ['kk','kk'],
    ['kl','kl'],
    ['kle','kle'],
    ['km','km'],
    ['kmr','kmr'],
    ['kn','kn'],
    ['knj','knj'],
    ['kpg','kpg'],
    ['kr','kr'],
    ['krs','krs'],
    ['ks','ks'],
    ['ksd','ksd'],
    ['ku','ku'],
    ['kw','kw'],
    ['ky','ky'],
    ['kyu','kyu'],
    ['la','la'],
    ['lb','lb'],
    ['lcm','lcm'],
    ['lcp','lcp'],
    ['lfn','lfn'],
    ['lg','lg'],
    ['li','li'],
    ['ln','ln'],
    ['lnd','lnd'],
    ['lo','lo'],
    ['lsi','lsi'],
    ['lt','lt'],
    ['ltg','ltg'],
    ['lu','lu'],
    ['lv','lv'],
    ['mad','mad'],
    ['mah','mah'],
    ['mai','mai'],
    ['mam','mam'],
    ['map','map'],
    ['mbb','mbb'],
    ['mdy','mdy'],
    ['me','me'],
    ['meu','meu'],
    ['mfe','mfe'],
    ['mg','mg'],
    ['mh','mh'],
    ['mhr','mhr'],
    ['mi','mi'],
    ['mk','mk'],
    ['ml','ml'],
    ['mn','mn'],
    ['mni','mni'],
    ['mos','mos'],
    ['mps','mps'],
    ['mr','mr'],
    ['mrj','mrj'],
    ['mrw','mrw'],
    ['ms','ms'],
    ['mt','mt'],
    ['mus','mus'],
    ['muv','muv'],
    ['mww','mww'],
    ['my','my'],
    ['nb','nb'],
    ['nch','nch'],
    ['nd','nd'],
    ['nds','nds'],
    ['ne','ne'],
    ['ng','ng'],
    ['ngl','ngl'],
    ['ngu','ngu'],
    ['nhg','nhg'],
    ['niu','niu'],
    ['no','no'],
    ['nop','nop'],
    ['nr','nr'],
    ['nso','nso'],
    ['ntm','ntm'],
    ['nv','nv'],
    ['ny','ny'],
    ['nyy','nyy'],
    ['oc','oc'],
    ['oj','oj'],
    ['ojb','ojb'],
    ['om','om'],
    ['ond','ond'],
    ['or','or'],
    ['os','os'],
    ['otq','otq'],
    ['pa','pa'],
    ['pag','pag'],
    ['pap','pap'],
    ['pck','pck'],
    ['pil','pil'],
    ['pmn','pmn'],
    ['poh','poh'],
    ['pot','pot'],
    ['ppk','ppk'],
    ['prk','prk'],
    ['ps','ps'],
    ['pss','pss'],
    ['ptu','ptu'],
    ['qu','qu'],
    ['quc','quc'],
    ['quh','quh'],
    ['quw','quw'],
    ['quz','quz'],
    ['qxr','qxr'],
    ['rm','rm'],
    ['rmn','rmn'],
    ['rn','rn'],
    ['ro','ro'],
    ['rom','rom'],
    ['rue','rue'],
    ['rug','rug'],
    ['rw','rw'],
    ['sa','sa'],
    ['sco','sco'],
    ['sd','sd'],
    ['se','se'],
    ['sg','sg'],
    ['shi','shi'],
    ['shn','shn'],
    ['shp','shp'],
    ['si','si'],
    ['sid','sid'],
    ['sk','sk'],
    ['sl','sl'],
    ['sm','sm'],
    ['sn','sn'],
    ['so','so'],
    ['spy','spy'],
    ['sq','sq'],
    ['sr','sr'],
    ['sr-Cyrl','sr-Cyrl'],
    ['sr-Latn','sr-Latn'],
    ['ss','ss'],
    ['ssd','ssd'],
    ['ssx','ssx'],
    ['st','st'],
    ['su','su'],
    ['sv','sv'],
    ['sw','sw'],
    ['swp','swp'],
    ['syc','syc'],
    ['syr','syr'],
    ['szl','szl'],
    ['ta','ta'],
    ['tbz','tbz'],
    ['te','te'],
    ['teo','teo'],
    ['tet','tet'],
    ['tex','tex'],
    ['tg','tg'],
    ['th','th'],
    ['ti','ti'],
    ['tig','tig'],
    ['tih','tih'],
    ['tk','tk'],
    ['tl','tl'],
    ['tlh','tlh'],
    ['tmh','tmh'],
    ['tn','tn'],
    ['to','to'],
    ['toi','toi'],
    ['tpi','tpi'],
    ['tpm','tpm'],
    ['tr','tr'],
    ['ts','ts'],
    ['tt','tt'],
    ['tvl','tvl'],
    ['tw','tw'],
    ['ty','ty'],
    ['tyv','tyv'],
    ['udm','udm'],
    ['ug','ug'],
    ['uk','uk'],
    ['umb','umb'],
    ['ur','ur'],
    ['usp','usp'],
    ['uz','uz'],
    ['ve','ve'],
    ['vi','vi'],
    ['vo','vo'],
    ['vun','vun'],
    ['wa','wa'],
    ['wal','wal'],
    ['war','war'],
    ['wlx','wlx'],
    ['wo','wo'],
    ['wrs','wrs'],
    ['wsk','wsk'],
    ['xal','xal'],
    ['xh','xh'],
    ['xsm','xsm'],
    ['yi','yi'],
    ['yo','yo'],
    ['yon','yon'],
    ['yua','yua'],
    ['zu','zu'],
    ['zyb','zyb'],
    ['zza','zza'],
    ['zze','zze'],
];

var langMap = new Map(items);
var langMapReverse = new Map(items.map(([standardLang, lang]) => [lang, standardLang]));

function supportLanguages() {
    return items.map(([standardLang, lang]) => standardLang);
}

function translate(query, completion) {
    // 记录日志
    $log.info('正在翻译: ' + query.text);

    // 获取配置的 API Key 和模型
    const apiToken = $option.apiToken;
    let model = $option.model || 'Qwen2.5-72B-Instruct';
    
    // 如果选择了自定义模型，使用自定义模型名称
    if (model === 'model-custom' && $option['model-custom']) {
        model = $option['model-custom'];
    }
    
    // 检查 API Key
    if (!apiToken) {
        completion({
            'error': {
                'type': 'api',
                'message': '未配置 API Token，请通过https://ai.gitee.com/dashboard/settings/tokens 创建 Token 并配置后使用'
            }
        });
        return;
    }
    
    // 确定目标语言的中文描述
    const languageMap = {
        'auto': '自动检测',
        'zh-Hans': '中文简体',
        'zh-Hant': '中文繁体',
        'yue': '粤语',
        'wyw': '文言文',
        'pysx': '拼音缩写',
        'en': '英语',
        'ja': '日语',
        'ko': '韩语',
        'fr': '法语',
        'de': '德语',
        'es': '西班牙语',
        'it': '意大利语',
        'ru': '俄语',
        'pt': '葡萄牙语',
        'nl': '荷兰语',
        'pl': '波兰语',
        'ar': '阿拉伯语',
        'ab': '阿布哈兹语',
        'ace': '亚齐语',
        'acr': '阿奇语',
        'acu': '阿丘雅语',
        'adh': '阿多拉语',
        'af': '南非语',
        'agr': '阿瓜鲁纳语',
        'ak': '阿肯语',
        'ake': '阿卡瓦伊语',
        'am': '阿姆哈拉语',
        'amk': '安拜语',
        'amu': '阿穆斯戈语',
        'an': '阿拉贡语',
        'any': '阿格尼桑维语',
        'as': '阿萨姆语',
        'ast': '阿斯图里亚斯语',
        'ata': '佩勒-阿塔语',
        'ay': '艾马拉语',
        'az': '阿塞拜疆语',
        'azb': '南阿塞拜疆语',
        'ba': '巴什基尔语',
        'bal': '俾路支语',
        'bam': '班巴拉语',
        'bba': '巴里巴语',
        'bch': '巴里亚语',
        'bdh': '巴卡语',
        'bdu': '奥罗科语',
        'be': '白俄罗斯语',
        'bem': '本巴语',
        'ber': '柏柏尔语',
        'bfa': '巴里语',
        'bg': '保加利亚语',
        'bho': '博杰普尔语',
        'bi': '比斯拉马语',
        'bm': '班巴拉语',
        'bn': '孟加拉语',
        'bno': '班通安隆语',
        'bnp': '波拉语',
        'bo': '藏语',
        'bqj': '班迪亚勒语',
        'bqp': '布萨语',
        'br': '布列塔尼语',
        'bs': '波斯尼亚语',
        'bsn': '巴拉萨纳语',
        'bus': '博科巴鲁语',
        'byn': '比林语',
        'byr': '伊普马语',
        'ca': '加泰罗尼亚语',
        'cak': '卡克奇克尔语',
        'cas': '齐马内语',
        'cbk': '查瓦卡诺语',
        'cbl': '布阿尔考钦语',
        'ccp': '查克玛语',
        'cdf': '茨鲁语',
        'ceb': '宿务语',
        'cfm': '法兰钦语',
        'ch': '查莫罗语',
        'che': '车臣语',
        'chm': '马里语（俄罗斯）',
        'chq': '瓦哈卡语',
        'chr': '切罗基语',
        'cjp': '卡韦卡尔语',
        'ckb': '中库尔德语',
        'cki': '喀克其奎语',
        'cnh': '哈卡钦语',
        'cni': '坎帕语',
        'cnr': '黑山语',
        'co': '科西嘉语',
        'cop': '科普特语',
        'cpb': '阿舍宁卡语',
        'crh': '克里米亚鞑靼语',
        'crs': '塞舌尔克里奥尔语',
        'cs': '捷克语',
        'csb': '卡舒比语',
        'ctd': '特丁钦语',
        'cv': '楚瓦什语',
        'cy': '威尔士语',
        'czt': '佐通钦语',
        'da': '丹麦语',
        'dik': '丁卡语',
        'dje': '哲尔马语',
        'djk': '恩都卡语',
        'dop': '卢克帕语',
        'dtp': '杜顺语',
        'duo': '东部卡加延-阿格塔语',
        'dv': '迪维西语',
        'dyu': '迪尤拉语',
        'dz': '宗喀语',
        'ee': '埃维语',
        'el': '希腊语',
        'enx': '恩舍特语',
        'eo': '世界语',
        'et': '爱沙尼亚语',
        'eu': '巴斯克语',
        'fa': '波斯语',
        'fi': '芬兰语',
        'fil': '菲律宾语',
        'fj': '斐济语',
        'fo': '法罗语',
        'fur': '弗留利语',
        'fuv': '尼日利亚富拉语',
        'fvr': '富尔语',
        'fy': '弗里斯兰语',
        'ga': '爱尔兰语',
        'gbi': '加莱拉语',
        'gbo': '北部格雷博语',
        'gd': '苏格兰盖尔语',
        'gil': '基里巴斯语',
        'gl': '加利西亚语',
        'gn': '瓜拉尼语',
        'gnw': '西部玻利维亚瓜拉尼语',
        'gof': '果发语',
        'gos': '格罗宁根方言',
        'gu': '古吉拉特语',
        'gub': '瓜哈哈拉语',
        'gur': '法拉法拉语',
        'gv': '马恩语',
        'ha': '豪萨语',
        'haw': '夏威夷语',
        'hbs': '塞尔维亚语-克罗地亚语',
        'he': '希伯来语',
        'hi': '印地语',
        'hil': '希利盖农语',
        'hlb': '亥比语',
        'hmn': '苗语',
        'hmo': '希里莫图语',
        'hr': '克罗地亚语',
        'hsb': '上索布语',
        'ht': '海地克里奥尔语',
        'hu': '匈牙利语',
        'hui': '胡里语',
        'hup': '胡帕语',
        'huv': '圣马特奥德马尔-瓦维语',
        'hwc': '夏威夷克里奥尔英语',
        'hy': '亚美尼亚语',
        'ia': '因特语',
        'iba': '伊班语',
        'id': '印尼语',
        'ie': '西方国际语',
        'ifa': '中部伊富高语',
        'ifb': '阿雅安伊富高语',
        'ify': '安蒂波洛伊富高语',
        'ig': '伊博语',
        'ikk': '伊卡语',
        'ilo': '伊洛卡诺语',
        'inh': '印古什语',
        'io': '伊多语',
        'iou': '图马伊鲁穆语',
        'is': '冰岛语',
        'iu': '伊努克提图特语',
        'izz': '伊兹语',
        'jac': '雅加达语',
        'jae': '雅比姆语',
        'jbo': '逻辑语',
        'jiv': '舒阿尔语',
        'jmc': '斯哈语',
        'jv': '印尼爪哇语',
        'jw': '爪哇语',
        'jy': '格鲁吉亚语',
        'ka': '格鲁吉亚语',
        'kab': '卡拜尔语',
        'kac': '景颇语',
        'kbh': '科奇语',
        'kbo': '克利科语',
        'kdx': '侗语',
        'kek': '玛雅语',
        'keo': '卡库瓦语',
        'kg': '刚果语',
        'kha': '卡西语',
        'ki': '基库尤语',
        'kj': '宽亚玛语',
        'kk': '哈萨克语',
        'kl': '格陵兰语',
        'kle': '库隆语',
        'km': '高棉语',
        'kmr': '北库尔德语',
        'kn': '卡纳达语',
        'knj': '阿卡特克语',
        'kpg': '卡平阿马朗伊语',
        'kr': '卡努里语',
        'krs': '格巴亚语',
        'ks': '克什米尔语',
        'ksd': '库阿努阿语',
        'ku': '库尔德语',
        'kw': '康沃尔语',
        'ky': '柯尔克孜语',
        'kyu': '西部克耶语',
        'la': '拉丁语',
        'lb': '卢森堡语',
        'lcm': '通加格语',
        'lcp': '西部拉威语',
        'lfn': '新共同语言',
        'lg': '卢干达语',
        'li': '林堡语',
        'ln': '林加拉语',
        'lnd': '弄巴湾语',
        'lo': '老挝语',
        'lsi': '勒期语',
        'lt': '立陶宛语',
        'ltg': '拉特加莱语',
        'lu': '卢巴卡丹加语',
        'lv': '拉脱维亚语',
        'mad': '马都拉语',
        'mah': '马绍尔语',
        'mai': '迈蒂利语',
        'mam': '马姆语',
        'map': '北部马姆语',
        'mbb': '西布基农马诺布语',
        'mdy': '马勒语',
        'me': '黑山语',
        'meu': '摩图语',
        'mfe': '毛里求斯克里奥尔语',
        'mg': '马尔加什语',
        'mh': '马绍尔语',
        'mhr': '马里语',
        'mi': '毛利语',
        'mk': '马其顿语',
        'ml': '马拉雅拉姆语',
        'mn': '蒙古语',
        'mni': '曼尼普尔语',
        'mos': '莫西语',
        'mps': '达迪比语',
        'mr': '马拉地语',
        'mrj': '山地马里语',
        'mrw': '马拉瑙语',
        'ms': '马来语',
        'mt': '马耳他语',
        'mus': '克里克语',
        'muv': '穆图凡语',
        'mww': '白苗语',
        'my': '缅甸语',
        'nb': '挪威布克莫尔语',
        'nch': '阿兹特克语',
        'nd': '北恩德贝勒语',
        'nds': '低地德语',
        'ne': '尼泊尔语',
        'ng': '恩敦加语',
        'ngl': '隆韦语',
        'ngu': '格雷罗纳瓦特尔语',
        'nhg': '纳瓦特尔语',
        'niu': '纽埃语',
        'no': '挪威语',
        'nop': '努曼干语',
        'nr': '南恩德贝勒语',
        'nso': '北索托语',
        'ntm': '纳特尼语',
        'nv': '纳瓦霍语',
        'ny': '齐切瓦语',
        'nyy': '尼亚库萨语',
        'oc': '奥克语',
        'oj': '奥杰布瓦语',
        'ojb': '奥吉布瓦语',
        'om': '奥罗莫语',
        'ond': '临高语',
        'or': '奥里亚语',
        'os': '奥塞梯语',
        'otq': '克雷塔罗奥托米语',
        'pa': '旁遮普语',
        'pag': '邦阿西楠语',
        'pap': '帕皮阿门托语',
        'pck': '派特语',
        'pil': '约姆语',
        'pmn': '帕姆语',
        'poh': '波孔奇语',
        'pot': '波塔瓦托米语',
        'ppk': '乌玛语',
        'prk': '佤语',
        'ps': '普什图语',
        'pss': '卡乌龙语',
        'ptu': '邦邦语',
        'qu': '克丘亚语',
        'quc': '基切语',
        'quh': '南玻利维亚克丘亚语',
        'quw': '盖丘亚语',
        'quz': '库斯科克丘亚语',
        'qxr': '卡纳尔高地-基丘亚语',
        'rm': '罗曼什语',
        'rmn': '罗姆语',
        'rn': '基隆迪语',
        'ro': '罗马尼亚语',
        'rom': '罗姆语',
        'rue': '卢森尼亚语',
        'rug': '罗维那语',
        'rw': '卢旺达语',
        'sa': '梵语',
        'sco': '低地苏格兰语',
        'sd': '信德语',
        'se': '北萨米语',
        'sg': '桑戈语',
        'shi': '希尔哈语',
        'shn': '掸语',
        'shp': '西皮沃语',
        'si': '僧伽罗语',
        'sid': '锡达莫语',
        'sk': '斯洛伐克语',
        'sl': '斯洛文尼亚语',
        'sm': '萨摩亚语',
        'sn': '修纳语',
        'so': '索马里语',
        'spy': '萨鲍特语',
        'sq': '阿尔巴尼亚语',
        'sr': '塞尔维亚语',
        'sr-Cyrl': '塞尔维亚语-西里尔文',
        'sr-Latn': '塞尔维亚语-拉丁文',
        'ss': '史瓦帝语',
        'ssd': '西罗伊语',
        'ssx': '桑贝里吉语',
        'st': '塞索托语',
        'su': '巽他语',
        'sv': '瑞典语',
        'sw': '斯瓦希里语',
        'swp': '苏奥语',
        'syc': '叙利亚语',
        'syr': '叙利亚语',
        'szl': '西里西亚语',
        'ta': '泰米尔语',
        'tbz': '迪塔马利语',
        'te': '泰卢固语',
        'teo': '特索语',
        'tet': '德顿语',
        'tex': '腾内特语',
        'tg': '塔吉克语',
        'th': '泰语',
        'ti': '提格雷尼亚语',
        'tig': '提格雷语',
        'tih': '蒂穆贡-穆鲁特语',
        'tk': '土库曼语',
        'tl': '菲律宾语',
        'tlh': '克林贡语',
        'tmh': '图阿雷格语',
        'tn': '茨瓦纳语',
        'to': '汤加语',
        'toi': '汤加语（赞比亚）',
        'tpi': '巴布亚皮钦语',
        'tpm': '坦普尔马语',
        'tr': '土耳其语',
        'ts': '聪加语',
        'tt': '鞑靼语',
        'tvl': '图瓦卢语',
        'tw': '契维语',
        'ty': '塔希提语',
        'tyv': '图瓦语',
        'udm': '乌德穆尔特语',
        'ug': '维吾尔语',
        'uk': '乌克兰语',
        'umb': '姆班杜语',
        'ur': '乌尔都语',
        'usp': '乌斯潘坦语',
        'uz': '乌兹别克语',
        've': '文达语',
        'vi': '越南语',
        'vo': '沃拉普克语',
        'vun': '文约语',
        'wa': '瓦隆语',
        'wal': '瓦拉莫语',
        'war': '瓦瑞语（菲律宾）',
        'wlx': '瓦利语',
        'wo': '沃洛夫语',
        'wrs': '瓦里斯语',
        'wsk': '瓦吉语',
        'xal': '卡尔梅克卫拉特语',
        'xh': '科萨语',
        'xsm': '格森语',
        'yi': '意第绪语',
        'yo': '约鲁巴语',
        'yon': '永贡语',
        'yua': '尤卡坦玛雅语',
        'zu': '祖鲁语',
        'zyb': '邕北壮语',
        'zza': '扎扎其语',
    };
    
    const targetLanguageName = languageMap[query.detectTo] || '目标语言';
    
    // 准备请求参数
    const requestObject = {
        url: 'https://ai.gitee.com/api/v1/chat/completions',
        method: 'POST',
        header: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + apiToken
        },
        body: {
            messages: [{
                role: "system",
                content: `请将以下内容翻译为${targetLanguageName}：` + query.text
            }],
            model: model
        }
    };
    
    // 发送请求
    $http.request({
        url: requestObject.url,
        method: requestObject.method,
        header: requestObject.header,
        body: requestObject.body,
        handler: function(response) {
            // 处理响应
            if (response.data && response.data.choices && response.data.choices.length > 0) {
                const translatedText = response.data.choices[0].message.content;
                
                // 返回翻译结果
                completion({
                    'result': {
                        'from': query.detectFrom,
                        'to': query.detectTo,
                        'toParagraphs': [translatedText]
                    }
                });
            } else {
                // 处理无效响应
                completion({
                    'error': {
                        'type': 'api',
                        'message': '未能获取有效的翻译结果'
                    }
                });
            }
        },
        error: function(error) {
            // 处理错误
            $log.error('翻译请求失败: ' + JSON.stringify(error));
            completion({
                'error': {
                    'type': 'network',
                    'message': '翻译请求失败: ' + error.localizedDescription
                }
            });
        }
    });
}
